package com.jby.autotest.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jby.autotest.common.ArrayUtils;
import com.jby.autotest.common.Constants;
import com.jby.autotest.model.BuyRecord;
import com.jby.autotest.model.Product;
import com.jby.autotest.model.Tender;
import com.jby.autotest.service.financial.PublishProductService;
import com.jby.autotest.service.financial.PublishYNJHOrSRDZOrYNPLUSService;
import com.jby.autotest.service.other.WholeProcessService;
import com.jby.autotest.service.pro_ynjh.BuyYNJHService;
import com.jby.autotest.service.pro_ynjh.OverDueForFinishWrapService;
import com.jby.autotest.service.tender.PublishSecondTenderService;
import com.jby.autotest.service.tender.impl.PublishFirstTenderServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/wholeProcess2")
public class WholeProcessController2 {

    private static final Logger logger = LogManager.getLogger(WholeProcessController2.class);

    @Autowired
    private PublishFirstTenderServiceImpl publishFirstTenderService;

    @Autowired
    private PublishYNJHOrSRDZOrYNPLUSService publishYNJHOrSRDZOrYNPLUSService;

    @Autowired
    private BuyYNJHService buyYNJHService;

    @Autowired
    private PublishProductService publishProductService;

    @Autowired
    private PublishSecondTenderService publishSecondTenderService;

    @Autowired
    private OverDueForFinishWrapService overDueForFinishWrapService;

    @Autowired
    private WholeProcessService wholeProcessService;

    @RequestMapping(value = "/saveWholeProcess2")
    @ResponseBody
    public Map<String, Object> saveWholeProcess(String[] items, Tender tender,
                                                String productTenderIdList, String tenderFullTenderIdList,
                                                String userEnsureOwnTenderTenderIdList, String tenderFinalCheckTenderIdList,
                                                String repaySecParentTenderIdList, String interestProductIdList,
                                                Product product, BuyRecord buyRecord){

        Map<String,Object> returnMap = new HashMap<>();
        boolean isTrue = false;
        Date date = new Date();
        Integer[] nums;

        List<Integer> tenderIdList = new ArrayList<>();
        List<Integer> productIdList = new ArrayList<>();
        ArrayUtils arrayUtils = new ArrayUtils();
        nums = arrayUtils.strArrayToIntArray(items);

        Arrays.sort(nums);
        int min = (int) Collections.min(Arrays.asList(nums));
//        int max = (int) Collections.max(Arrays.asList(nums));

        if(min == 2){
            //如果最小值是购买发布，则将前台传入的tid放入tenderIdList
//            tenderIdList = productTenderIdList;
        }else if(min == 3){
//            tenderIdList =
        }else if(min == 4){
//            tenderIdList =
        }else if(min == 5){
//            tenderIdList =
        }else if(min == 6){
//            tenderIdList =
        }

        try{
            for(int i = min; i <= nums.length; i++){
                if(i == 1){
                    if(tender.getBorrowTime().equals("1")){
                        tenderIdList = publishFirstTenderService.publishFirstTender(tender);
                    }
                    else if(tender.getBorrowTime().equals("2")){
                        tenderIdList = publishSecondTenderService.publishSecondTender(tender);
                    }
                }
                //发布，购买
                else if(i == 2 && !tenderIdList.isEmpty()){
                    Map<String, Object> map = publishYNJHOrSRDZOrYNPLUSService.publishProducts(tenderIdList, product);
                    isTrue = (Boolean) map.get("isTrue");
                    buyRecord.setProductId(555558);
                    buyRecord.setProductType("1");
                    interestProductIdList = "555558";
                    int productType = Integer.parseInt(buyRecord.getProductType());
                    buyYNJHService.buyYNJH(buyRecord);

                    if(Constants.PRODUCT_TYPE_YNJH == productType) {
                        isTrue = buyYNJHService.buyYNJH(buyRecord);
                    }
                    else if(Constants.PRODUCT_TYPE_SRDZ == productType){
//                        isTrue = buySRDZService.buySRDZWithOneInvestorAndMultipleProduct(buyRecord);
                    }
                    else if(Constants.PRODUCT_TYPE_ZMKH == productType){
//                        isTrue =  buyZMKHService.buyZMKH(buyRecord);
                    }
                    else if(Constants.PRODUCT_TYPE_YNPLUS == productType){
//                        isTrue = buyYNJHPLUSService.buyYNJHPlus(buyRecord);
                    }
                }
                //满标
                else if(i == 3 && !tenderIdList.isEmpty()){
                    tenderIdList = publishProductService.pushTenderFull(tenderIdList);
                }
                else if(i == 4 && !tenderIdList.isEmpty()){
                    publishFirstTenderService.userEnsureOwnTender(tenderIdList, tender.getTenderLoanSource());
                }
                else if(i == 5 && !tenderIdList.isEmpty()){
                    publishFirstTenderService.tenderFinalCheck(tenderIdList);
                }
                else if(i == 6 && !tenderIdList.isEmpty()){
                    publishSecondTenderService.repaySecParent(tenderIdList);
                }
                else if(i == 7 && !productIdList.isEmpty()){
                    overDueForFinishWrapService.overDueForFinishWrap(product);
                }
            }

            returnMap.put("status","success");
        }catch (Exception e){
            logger.error("WholeProcessController-->saveWholeProcess: " + e);
            returnMap.put("status","fail");
            return returnMap;
        }
        return returnMap;
    }

    @RequestMapping(value = "/selectAll")
    @ResponseBody
    public Map<String, Object> selectAll(String borrowerPhone, String tenderTitle, String investorPhone,
                                         @RequestParam(required = false, defaultValue = "1")int page,
                                         @RequestParam(required = false, defaultValue = "10")int rows){

        Map<String,Object> map = new HashMap<>();

//        private Integer productId;
//
//        private String productType;
//
//        private String periodClose;
//
//        private Tender tender;
//
//        private BuyRecord buyRecord;

        Product product = new Product();

        Tender tender = new Tender();
        BuyRecord buyRecord = new BuyRecord();

        if(borrowerPhone != null && borrowerPhone.length() > 0){
            tender.setBorrowerPhone(borrowerPhone);
        }
//        if(tenderId != null){
//            tender.setTenderId(tenderId);
//        }
        if(tenderTitle != null && tenderTitle.length() > 0){
            tender.setTenderTitle(tenderTitle);
        }
        if(investorPhone != null && investorPhone.length() > 0){
            buyRecord.setInvestorPhone(investorPhone);
        }

        product.setTender(tender);
//        product.setBuyRecord(buyRecord);

        try {
//            System.out.println("1");
//            System.out.println("2");
            PageHelper.startPage(page,rows);
            List<Product> productList = wholeProcessService.selectAll(product);
            PageInfo<Product> pageInfo = new PageInfo<>(productList);
            System.out.println(JSON.toJSONString(productList));
            map.put("total",pageInfo.getTotal());
            map.put("rows",productList);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
